# coding=utf-8
import os
import requests
from apig_sdk import signer

if __name__ == "__main__":
    sig = signer.Signer()
    # Set the AK/SK to sign and authenticate the request.
    # 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
    # 本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
    """
    1.	对应“飞服notam数据”的需求，请求URL为:
    2.	https://4f4c043bb8544e4ca9a8b639bdae3c39.apic.cn-north-4.huaweicloudapis.com/v1.0/api_intelligent_tower_get_roma_notam
    3.	请求方式：GET
    4.	秘钥
    Key：c460a6e1-d73d-4764-bb9d-196df06a464d
    Secret：a18463badf7c7daa9c85a75adeb1b07d539cfef4b82584d0654148f77c931f21

    """
    sig.Key = "c460a6e1-d73d-4764-bb9d-196df06a464d"
    sig.Secret = "a18463badf7c7daa9c85a75adeb1b07d539cfef4b82584d0654148f77c931f21"

    # The following example shows how to set the request URL and parameters to query a VPC list.
    # Set request Endpoint.
    # Specify a request method, such as GET, PUT, POST, DELETE, HEAD, and PATCH.
    # Set request URI.
    # Set parameters for the request URL.
    r = signer.HttpRequest(
        "GET",
        "http://192.168.120.220/v1.0/api_intelligent_tower_get_roma_notam",
    )
    # Add header parameters, for example, x-domain-id for invoking a global service and x-project-id for invoking a project-level service.
    r.headers = {"content-type": "application/json"}
    # Add a body if you have specified the PUT or POST method. Special characters, such as the double quotation mark ("), contained in the body must be escaped.
    r.body = "verify=False"
    sig.Sign(r)
    print(r.headers["X-Sdk-Date"])
    print(r.headers["Authorization"])

    import json

    print(r.__dict__)
    print(f"headers: {r.headers}")
    # dict转json

    url = r.scheme + "://" + r.host + r.uri
    print(url)
    req = requests.get(
        "http://127.0.0.1/v1.0/api_intelligent_tower_get_roma_notam",
        headers=r.headers,
        data=r.body,
    )
    print(req.content)

    resp = requests.request(
        r.method, r.scheme + "://" + r.host + r.uri, headers=r.headers, data=r.body
    )
    print(resp.status_code, resp.reason)
    print(resp.content)
    print(resp.__dict__)
